import { createRouter, createWebHistory } from 'vue-router'
import PkRankIndexView from '../views/pk/rank/PkRankIndexView'
import AiChoiceView from '../views/pk/ai/AiChoiceView'
import PkChoiceView from '../views/pk/PkChoiceView'
import NotFoundView from '../views/error/NotFoundView'
import RanklistIndexView from '../views/ranklist/RanklistIndexView'
import UserBotIndexView from '../views/user/bot/UserBotIndexView'
import RecordIndexView from '../views/record/RecordIndexView'
import UserAccountLoginView from '../views/user/account/UserAccountLoginView.vue'
import UserAccountRegisterView from '../views/user/account/UserAccountRegisterView.vue'
import store from "../store/index"
import RecordContentView from '../views/record/RecordContentView'
import FriendPkView from '../views/pk/friend/FriendPkView'
const routes = [
  {
    path:'/',
    name:'home',
    redirect:'/pkchoice/',
    meta:{
      requersAuth:true,
    }
  },  
  {
    path:'/aichoice/',
    name:'ai_choice',
    component:AiChoiceView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path:'/pkchoice/',
    name:'pk_choice',
    component:PkChoiceView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path:'/pk/rank/',
    name:'pk_rank_index',
    component:PkRankIndexView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path:'/pk/friend/',
    name:'pk_friend_index',
    component:FriendPkView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path:'/404/',
    name:'notfound',
    component:NotFoundView,
    meta:{
      requersAuth:false,
    }
  },
  {
    path:'/ranklist/',
    name:'ranklist_index',
    component:RanklistIndexView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path:'/user/bot/',
    name:'user_bot_index',
    component:UserBotIndexView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path:'/record/',
    name:'record_index',
    component:RecordIndexView,
    meta:{
      requersAuth:true,
    }
  },
  {
    path: "/record/:recordId/",
    name: "record_content",
    component: RecordContentView,
    meta: {
      requestAuth: true,
    }
  },

  {
    path:'/user/account/lgoin/',
    name:'user_account_login',
    component:UserAccountLoginView,
    meta:{
      requersAuth:false,
    }
  },
  {
    path:'/user/account/register/',
    name:'user_account_register',
    component:UserAccountRegisterView,
    meta:{
      requersAuth:false,
    }
  },
  {
    path:"/:catchAll(.*)",
    redirect:"/404/"
  },
  
  
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.meta.requersAuth && !store.state.user.is_login){
    next({name:"user_account_login"});
  }else{
    next();
  }
})
export default router
